#ifndef CLIENT_H
#define CLIENT_H

#include <QObject>
#include <QtNetwork/QTcpSocket>
#include <QDebug>
#include <QMap>

#include "stdafx.h"
#include "types.h"
#include "device.h"

class Client : public QObject
{
    Q_OBJECT
    Q_PROPERTY_AUTO(quint8, cur_device_index)
    Q_PROPERTY_READONLY_AUTO(Types::SocketStatus, status)
    Q_PROPERTY_READONLY_AUTO(QString, hostIp)
    Q_PROPERTY_READONLY_AUTO(QList<Device *>, devices)

public:
    explicit Client(QObject *parent = nullptr);
    Q_INVOKABLE void connectToServer(const QString &host, quint16 port);
    Q_INVOKABLE void disconnectFromServer();
    Q_INVOKABLE void aquireResult(quint8 deviceId, quint32 resultIndex);
    void sendToServer(const QByteArray &data);
    QTcpSocket *socket;

private:
    void onConnected();
    void onDisconnected();
    void onReadyRead();
    void onError(QAbstractSocket::SocketError socketError);
    QMap<quint8, Device*> device_map;

signals:
    void resultReceived(quint8 deviceId, quint32 resultIndex);
    void connectedFailed(const QString &error);
    void connected();
    void disconnected();
};

#endif // CLIENT_H
